package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallFootprint;
import com.ctsi.ssdc.domain.LitemallFootprintExample;
import com.ctsi.ssdc.service.LitemallFootprintService;
import com.ctsi.ssdc.repository.LitemallFootprintRepository;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallFootprint.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallFootprintServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallFootprintRepository, LitemallFootprint, Integer, LitemallFootprintExample> 
	implements LitemallFootprintService {

    @Autowired
    LitemallFootprintRepository litemallFootprintRepository;


    /**
    * GET  /litemallFootprints : get the litemallFootprints firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallFootprint> findFirstStringColumn(String str,Pageable pageable){
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallFootprintExample litemallFootprintExample = new LitemallFootprintExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallFootprintExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallFootprintExample.createCriteria();
        } 
        List<LitemallFootprint>  data = litemallFootprintRepository.selectByExample(litemallFootprintExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallFootprintRepository.countByExample(litemallFootprintExample);
        }
        return new PageResult<LitemallFootprint>(data,count,count);
    }

    @Override
    public LitemallFootprint findById(Integer userId, Integer footprintId) {
        LitemallFootprintExample example = new LitemallFootprintExample();
        example.or().andIdEqualTo(footprintId).andUserIdEqualTo(userId).andDeletedEqualTo(false);
        return litemallFootprintRepository.selectOneByExample(example);
    }

    @Override
    public LitemallFootprint findById(Integer id) {
        return litemallFootprintRepository.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(Integer id) {
        litemallFootprintRepository.logicalDeleteByPrimaryKey(id);
    }

    @Override
    public List<LitemallFootprint> queryByAddTime(Integer userId, Integer page, Integer limit) {
        LitemallFootprintExample example = new LitemallFootprintExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        example.setOrderByClause(LitemallFootprint.Column.addTime.desc());
        PageHelper.startPage(page, limit);
        return litemallFootprintRepository.selectByExample(example);
    }

    @Override
    public void add(LitemallFootprint footprint) {
        footprint.setAddTime(ZonedDateTime.now());
        footprint.setUpdateTime(ZonedDateTime.now());
        litemallFootprintRepository.insertSelective(footprint);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
